# !/bin/bash -x

set -e -o pipefail

# verify Emscripten version
emcc -v

WEB_CAPTURE_PATH=$(pwd)
FFMPEG_PATH=$(pwd)/emccbuild
TOTAL_MEMORY=512MB

mkdir -p tmp

echo $WEB_CAPTURE_PATH

echo $FFMPEG_PATH

ARGS=(
  # $WEB_CAPTURE_PATH/src/capture.c $FFMPEG_PATH/lib/libavformat.a $FFMPEG_PATH/lib/libavcodec.a $FFMPEG_PATH/lib/libswscale.a $FFMPEG_PATH/lib/libavutil.a
  $WEB_CAPTURE_PATH/src/CCaptureProcess.cpp $WEB_CAPTURE_PATH/src/CCaptureVideoFrames.cpp $FFMPEG_PATH/lib/libavformat.a $FFMPEG_PATH/lib/libavcodec.a $FFMPEG_PATH/lib/libswscale.a $FFMPEG_PATH/lib/libavutil.a
  # $WEB_CAPTURE_PATH/src/test.c $FFMPEG_PATH/lib/libavformat.a $FFMPEG_PATH/lib/libavcodec.a $FFMPEG_PATH/lib/libswscale.a $FFMPEG_PATH/lib/libavutil.a
  -O3
  -I "$FFMPEG_PATH/include"
  -s WASM=1
  -s MODULARIZE=1
  -s EXPORT_ES6=1
  -s ENVIRONMENT='web'
  -s TOTAL_MEMORY=$TOTAL_MEMORY
  -s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap", "FS"]'
  -s EXPORTED_FUNCTIONS='["_main", "_generateVideoFrames", "_getImage", "_freeImages"]'
  -s ASSERTIONS=1
  -s SINGLE_FILE=1
  -s BINARYEN_ASYNC_COMPILATION=0
  -s SAFE_HEAP=1
  -s ALLOW_MEMORY_GROWTH=1
  -o $WEB_CAPTURE_PATH/tmp/video-frame-capture.js
)

emcc "${ARGS[@]}"

